﻿using System;
using System.IO;

namespace ExcelToJson
{
    class ExcelToJsonClass
    {
        private string mExcelsPath = @"Excels";
        private string mClientClassesPath = @"Client/Classes";
        private string mClientJsonsPath = @"Client/Jsons";
        private string mServerClassesPath = @"Server/Classes";
        private string mServerJsonsPath = @"Server/Jsons";


        private ClassGenerator mClientClassGenerator = new ClassGenerator();
        private JsonGenerator mClientJsonGenerator = new JsonGenerator();
        private ClassGenerator mServerClassGenerator = new ClassGenerator();
        private JsonGenerator mServerJsonGenerator = new JsonGenerator();

        public void ExportExcel()
        {
            FileSystemInfo[] filesArray = GetAllExcelPath();
            if (filesArray != null && filesArray.Length > 0)
            {
                DeleteAllFilesByPath(mClientClassesPath);
                DeleteAllFilesByPath(mClientJsonsPath);
                DeleteAllFilesByPath(mServerClassesPath);
                DeleteAllFilesByPath(mServerJsonsPath);

                mClientClassGenerator.Init(mClientClassesPath, PlatformType.Client);
                mClientJsonGenerator.Init(mClientJsonsPath, PlatformType.Client);
                mServerClassGenerator.Init(mServerClassesPath, PlatformType.Server);
                mServerJsonGenerator.Init(mServerJsonsPath, PlatformType.Server);

                foreach (FileSystemInfo fileSystemInfo in filesArray)
                {
                    if (!fileSystemInfo.Name.Contains("~"))
                    {
                        if (!FileIsOpen(fileSystemInfo.FullName))
                        {
                            Console.WriteLine(string.Format("{0}打开中，配置生成失败！！！", fileSystemInfo.Name));
                            return;
                        }

                        Console.WriteLine(fileSystemInfo.FullName);
                        mClientClassGenerator.GenerateScript(fileSystemInfo);
                        mClientJsonGenerator.GenerateJson(fileSystemInfo);

                        mServerClassGenerator.GenerateScript(fileSystemInfo);
                        mServerJsonGenerator.GenerateJson(fileSystemInfo);
                    }
                }

                Console.WriteLine("配表导出成功");
            }
        }

        private FileSystemInfo[] GetAllExcelPath()
        {
            if (Directory.Exists(mExcelsPath))
            {
                DirectoryInfo directory = new DirectoryInfo(mExcelsPath);
                FileSystemInfo[] filesArray = directory.GetFileSystemInfos();
                return filesArray;
            }
            else
            {
                Console.WriteLine("Excels目录不存在");
                return null;
            }
        }

        private void DeleteAllFilesByPath(string pPath)
        {
            if (Directory.Exists(pPath))
            {
                DirectoryInfo directory = new DirectoryInfo(pPath);
                FileSystemInfo[] filesArray = directory.GetFileSystemInfos();
                foreach (FileSystemInfo fileSystemInfo in filesArray)
                {
                    File.Delete(fileSystemInfo.FullName);
                }
            }
        }

        private bool FileIsOpen(string pConfigPath)
        {
            try
            {
                File.OpenRead(pConfigPath);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
